<template>
  <div class="cart-list">
    <div class="cart-list-main">
      <div class="cart-list-item top" v-for="item in carLists" :key="item.id">
        <div
          :class="isChecked"
            
          
          ref="circle"
          class="circle"
          @click="carListClick(item)"
        ></div>
        <img :src="item.list_pic_url" alt="" />
        <div class="cart-main">
          <div class="top-right">
            <p class="title">{{ item.goods_name }}</p>
            <p class="count">{{ item.retail_price | priceFilter }}</p>
          </div>
          <div class="counts">x{{ item.number }}</div>
        </div>
      </div>
    </div>
    <!-- <div>{{ this.$store.state.count }}</div> -->
  </div>
</template>

<script>
// import {mapState, mapMutations, mapGetters, mapActions} from 'vuex'

export default {
  props: {
    carLists: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      cartData: this.$store.state,
      currentIndex: null,
    };
  },
  computed: {
    isChecked() {
      if (this.$store.state.cartObj.carList) {
        return this.$store.state.cartObj.carList[this.currentIndex].isChecked ? 'actives': ''
      }else {
         return ""
      }   
    }
  },
  methods: {
    carListClick(item) {
      // console.log(item.id)
      // this.$refs.circle.
      let index = this.carLists.findIndex((items) => items.id == item.id);
      this.currentIndex = index;

      let isCheckIndex = this.$store.state.cartObj.carList ? this.$store.state.cartObj.carList.findIndex(
        (items) => items.id == item.id
      ) : null;
      // console.log(this.$store.state.cartObj.cartList[1].isChecked) // 还需要判断一下
      if (this.$store.state.cartObj.carList !== null) {
        console.log(this.$store.state.cartObj)
        // return this.$store.state.cartObj.carList[this.currentIndex].isChecked = !this.$store.state.cartObj.carList[this.currentIndex].isChecked
      }
      console.log(this.$store.state.cartObj)
      this.$emit("carListClick", item.id, );
    },
  },
  filters: {
    priceFilter(val) {
      return "¥" + val;
    },
  },
};
</script>

<style lang="scss" scoped>
.cart-list-main {
  padding: 10px;
  .top {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    .circle {
      width: 20px;
      height: 20px;
      background: url(~@/assets/icon/checkbox.png) no-repeat 0 -152px; // -152  -101活跃时图片
      background-size: 20px;
    }
    div.actives {
      width: 20px;
      height: 20px;
      background: url(~@/assets/icon/checkbox.png) no-repeat 0 -101px; // -152  -101活跃时图片
      background-size: 20px;
    }

    img {
      width: 88px;
      margin: 0 5px 0 0px;
    }
    .cart-main {
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: space-between;
      margin: 0 10px;
      .top-right {
        display: flex;
        // height: 100%;
        flex-direction: column;
        justify-content: space-around;
        .title {
          // color: #b4282d;
          margin: 10px 0;
        }
      }
      .counts {
        margin-right: 10px;
      }
    }
  }
}
</style>